package com.labofjet.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.labofjet.dto.ADTO;
import com.labofjet.entity.A;
import com.labofjet.entity.APK;
import com.labofjet.entity.Student;
import com.labofjet.repository.module1.ARepository;
import com.labofjet.repository.module2.PhoneRepository;
import com.labofjet.service.TestService;
import com.labofjet.web.ContextDTO;
import com.labofjet.web.enumeration.EState;

@Controller
public class TestController {
    Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;
    @Autowired
    private ARepository arepo;
    @Autowired
    private PhoneRepository phoneRepository;

    @RequestMapping("/test")
    @ResponseBody
    public String index(long id) {
        log.debug("hello {}", "jet");
        return testService.findById(id).toString();
    }

    @RequestMapping("/test2")
    @ResponseBody
    public String index2(String name) {
        return testService.findByName(name).toString();
    }

    @RequestMapping("/test3")
    @ResponseBody
    public Object index3(String name) {
        return testService.findStudentByPhonesPhoneName(name).toString();
    }

    @RequestMapping("/test4")
    @ResponseBody
    public Object index4(long id) {
        return testService.deleteStudentById(id).toString();
    }

    @RequestMapping("/test5")
    @ResponseBody
    public Object index5(long num) {
        Page<Student> page = testService.findAll(new PageRequest(1, (int) num));
        return page.getContent().toString();
    }

    @RequestMapping("/test6")
    @ResponseBody
    public Object index6() {
        List<Student> page = testService.findByIds(Arrays.asList(1l, 2l));
        return page.toString();
    }

    @RequestMapping("/test7")
    @ResponseBody
    public Object index7() {
        List<Student> page = testService.findTop2(new PageRequest(1, 1));
        return page.toString();
    }

    @RequestMapping("/test8")
    @ResponseBody
    public Object index8() {
        List<Student> page = testService.findTop3ByOrderByName(new PageRequest(1, 1));
        return page.toString();
    }

    @RequestMapping("/test9")
    @ResponseBody
    public Object index9() {
        Student s = testService.saveOne();
        return s.toString();
    }

    @RequestMapping("/test10")
    @ResponseBody
    public Object index10() {
        // 删除孤子
        Student s = testService.saveOne();
        Student newStu = new Student();
        newStu.setId(s.getId());
        newStu.setName("test10");
        newStu.setPhones(new ArrayList());
        testService.updateOne(newStu);
        return null;
    }

    @RequestMapping("/test11")
    @ResponseBody
    public Object index11() {
        // 测试更新
        Student s = testService.findById(2);
        s.setName(s.getName() + "_" + s.getId());
        s.setId(1);
        testService.saveOne(s);
        return null;
    }

    @RequestMapping("/test12")
    @ResponseBody
    public void index12() {
        // 测试主键
        // 结论:判断2个对象是不是同一个不是通过equals或者hashcode判断的,可能是通过SQL来判断的,有记录就是需要update,没有就是insert
        // A a = arepo.findOne(new APK("1","name1"));
        "1".equals("2");
        A a2 = new A();
        a2.setId(new APK("3", "name3"));
        a2.setAge("23");
        arepo.save(a2);
    }

    @RequestMapping("/test13")
    @ResponseBody
    public Object index13() {
        // 测试存储过程
        // Object o = arepo.alias3(123);
        // return null;
        Object[] o = arepo.mytest();
        return null;
    }

    @RequestMapping("/test14")
    @ResponseBody
    public Object index14(@RequestBody ContextDTO a) {
        ContextDTO dto = new ContextDTO();
        return dto;
    }

    /**
     * 测试前后台数据交互格式统一
     * 
     * @param context
     * @return
     * @author jiangyz
     */
    @RequestMapping("/test15")
    @ResponseBody
    public Object index15(@RequestBody ContextDTO context) {
        A a = context.getDTOFromData("aName", A.class);
        ContextDTO dto = new ContextDTO();
        dto.putData("aDataName", a);
        dto.setStateCode(EState.SUCCESS.getCode());
        throw new ArrayIndexOutOfBoundsException();
        //throw new BusinessException("999", "缴费人数为负数");
        //return dto;
    }

    /**
     * 测试原生SQL
     * 
     * @param context
     * @return
     * @author jiangyz
     */
    @RequestMapping("/test16")
    @ResponseBody
    public Object index16(ContextDTO context) {
        HashMap map = new HashMap<String, String>();
        map.put("age", "23");
        Object o = arepo.executeNativeQuerySQLReturnObject("select * from a where age = :age", map, ADTO.class);
        context.putData("obj", o);
        return context;
    }
    
    @RequestMapping("/test17")
    @ResponseBody
    public Object index17(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getHeaderNames();
        response.getWriter();
        return null;
    }
}
